package com.xinchuang.config;

import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.fiter.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 解决跨域问题
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Value("${info-trans.profile}")
    private String fileDir;

    @Resource
    private KeyWordSystem keyWordSystem;

    @Bean
    public AuthenticationInterceptor authenticationInterceptor(){
        return new AuthenticationInterceptor();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文件映射
        registry.addResourceHandler("/image/**").addResourceLocations("file:" + fileDir + "/image/");
        registry.addResourceHandler("/zip/**").addResourceLocations("file:" + fileDir + "/zip/");
        registry.addResourceHandler("/tif/**").addResourceLocations("file:" + fileDir + "/tif/");
        registry.addResourceHandler("/toTif/**").addResourceLocations("file:" + fileDir + "/toTif/");
        registry.addResourceHandler("/tifElse/**").addResourceLocations("file:" + fileDir + "/tifElse/");
        registry.addResourceHandler("/type/**").addResourceLocations("classpath:/type/");
        //体系系统文件映射
        registry.addResourceHandler("/icon/**").addResourceLocations("file:" + keyWordSystem.getFILE_URL() + "/icon/");
        registry.addResourceHandler("/doc/**").addResourceLocations("file:" + keyWordSystem.getFILE_URL() + "/doc/");
        registry.addResourceHandler("/sign/**").addResourceLocations("file:" + keyWordSystem.getFILE_URL() + "/sign/");
        registry.addResourceHandler("/file/**").addResourceLocations("file:" + keyWordSystem.getFILE_URL() + "/file/");
        registry.addResourceHandler("/default/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathPatterns = new ArrayList<String>();
        excludePathPatterns.add("/default/default.jpg");
        excludePathPatterns.add("/icon/**");
        excludePathPatterns.add("/sign/**");
        excludePathPatterns.add("/file/**");
        excludePathPatterns.add("/image/**");
        excludePathPatterns.add("/zip/**");
        excludePathPatterns.add("/type/**");
        excludePathPatterns.add("/swagger**/**");
        excludePathPatterns.add("/webjars/**");
        excludePathPatterns.add("/v3/**");
        excludePathPatterns.add("/doc.html");
        excludePathPatterns.add("/public/**");
        excludePathPatterns.add("/mercator/**");
        excludePathPatterns.add("/websocket/**");
        excludePathPatterns.add("/office/**");
        excludePathPatterns.add("/tif/**");
        excludePathPatterns.add("/toTif/**");
        excludePathPatterns.add("/tifElse/**");
        excludePathPatterns.add("/doc/**");
        registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**").excludePathPatterns(excludePathPatterns);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
//                        .allowedOrigins("*")
                        .allowedOriginPatterns("*")
                        .allowCredentials(true)
                        .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
                        .maxAge(3600);
            }
        };
    }
}
